const path = require("path");
const const_var = require("./src/const/var.js");
const cdn = require("./src/const/cdn.js");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
// process.env.NODE_ENV === 'development' ? true : false
// const CSSSplitWebpackPlugin = require("css-split-webpack-plugin")["default"];
const isProduction = process.env.NODE_ENV !== "development";
// 本地环境是否需要使用cdn
const devNeedCdn = false;
// cdn链接

module.exports = {
  publicPath: const_var.BASEURL,
  productionSourceMap: false,
  lintOnSave: true,
  // 配置代理
  devServer: {
    port: 8088,
    proxy: {
      "/": {
        // target: "http://10.1.28.147:9999", // 老梁公司服务器
        target: "http://10.1.65.122:9999", // 测试服务器
        // target: "http://10.1.28.89:9999/",
        changeOrigin: true,
        ws: false
      }
    }
  },
  // css: {
  //   // 开启CSS样式分离
  //   extract: true
  // },
  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: [
        // 全局less文件
        path.resolve(__dirname, "./src/styles/vars.less")
      ]
    }
  },
  // 兼容低版本浏览器设置
  configureWebpack: config => {
    config.entry.app = ["babel-polyfill", "./src/main.js"];
    // 去掉console，debugger
    if (process.env.NODE_ENV === "production") {
      config.optimization.minimizer[0].options.terserOptions.compress = {
        warnings: false,
        drop_console: true,
        drop_debugger: true,
        pure_funcs: ["console.log"]
      };
    }
    // 用cdn方式引入，则构建时要忽略相关资源
    if (isProduction || devNeedCdn) {
      config.externals = cdn.externals;
    }
    // 公共代码抽离
    config.optimization = {
      splitChunks: {
        cacheGroups: {
          vendor: {
            chunks: "all",
            test: /node_modules/,
            name: "vendor",
            minChunks: 1,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 100
          },
          common: {
            chunks: "all",
            test: /[\\/]src[\\/]js[\\/]/,
            name: "common",
            minChunks: 2,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 60
          },
          styles: {
            name: "styles",
            test: /\.(sa|sc|c)ss$/,
            chunks: "all",
            enforce: true
          },
          runtimeChunk: {
            name: "manifest"
          }
        }
      }
    };
    // webpack打包速度优化
    config.plugins.push(
      new CompressionWebpackPlugin({
        test: /\.js$|\.html$|\.css/
      })
    );

    // 兼容ie9的样式切割
    // config.plugins.push(
    //   new CSSSplitWebpackPlugin({
    //     size: 4000,
    //     filename: "css/[name]-[part].[ext]",
    //   })
    // );
  },
  chainWebpack: config => {
    // 打包分析工具
    // config
    //   .plugin("webpack-bundle-analyzer")
    //   .use(require("webpack-bundle-analyzer").BundleAnalyzerPlugin);
    config.plugin("html").tap(args => {
      // 生产环境或本地需要cdn时，才注入cdn
      if (isProduction || devNeedCdn) {
        args[0].cdn = cdn;
      }
      return args;
    });
  },
  // 指定对第三方组件也进行babel-polyfill处理
  transpileDependencies: ["resize-detector"]
};
